package com.kjsb.controller;

import com.kjsb.entity.Expert;
import com.kjsb.entity.User;
import com.kjsb.service.ExpertService;
import com.kjsb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/experts")
public class ExpertController {
    @Autowired
    private ExpertService service;

    /**
     * 查询所有专家用户
     * @return Result结果集
     */
    @GetMapping
    public Result getAllExpert(){
        List<Expert> list = service.getAllExperts();
        Integer code=list != null ? Code.GET_OK : Code.GET_ERR;
        String msg= list != null ? "查询成功！" : "数据查询失败，请重试！";
        return new Result(code,list,msg);
    }

    /**
     * 删除专家用户
     * @return Result结果集
     */
    @DeleteMapping("/{id}")
    public Result deleteExpert(@PathVariable Integer id){
        boolean i =service.deleteExperts(id);
        Integer code=i? Code.DELETE_OK : Code.DELETE_ERR;
        String msg= i? "删除成功！" : "删除失败，请重试！";
        return new Result(code,i,msg);
    }

    /**
     * 修改专家用户
     * @param expert 专家用户信息
     * @return Result结果集
     */
    @PutMapping
    public Result updateExpert(@RequestBody Expert expert){
        boolean i = service.updateExperts(expert);
        Integer code=i == true ? Code.UPDATE_OK : Code.UPDATE_ERR;
        String msg= i == true ? "修改成功！" : "修改失败，请重试！";
        return new Result(code,i,msg);
    }

    /**
     *根据id查询专家用户
     * @return Result结果集
     */
    @GetMapping("/{id}")
    public Result getByEId(@PathVariable Integer id){
        Expert expert = service.getByEId(id);
        Integer code=expert != null ? Code.GET_OK : Code.GET_ERR;
        String msg= expert != null ? "查询成功！" : "数据查询失败，请重试！";
        return new Result(code,expert,msg);
    }
    /**
     *根据uid查询专家用户
     * @return Result结果集
     */
    @GetMapping("/getByUid/{uid}")
    public Result getByUid(@PathVariable Integer uid){
        Expert expert = service.getByUid(uid);
        Integer code=expert != null ? Code.GET_OK : Code.GET_ERR;
        String msg= expert != null ? "查询成功！" : "数据查询失败，请重试！";
        return new Result(code,expert,msg);
    }
}
